package com.hqyj.springbootdemo01.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.springbootdemo01.user.dto.UserInfo;
import com.hqyj.springbootdemo01.user.dto.UserPageInfo;
import com.hqyj.springbootdemo01.user.dto.UserRole;
import com.hqyj.springbootdemo01.user.entity.User;
import com.hqyj.springbootdemo01.user.mapper.UserMapper;
import com.hqyj.springbootdemo01.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sdx2009
 * @since 2022-08-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserInfo(UserInfo userInfo) {


        QueryWrapper<User> qw = new QueryWrapper<>();
        //        效果比如：where userName='zhangsan' and password='123456'
//        qw.eq("userName",userInfo.getUserName()).eq("password",userInfo.getPassword());
//      效果比如：where userName='zhangsan' or password='123456'
        //        qw.eq("userName",userInfo.getUserName()).or().eq("password",userInfo
        //        .getPassword());

//        效果比如：where userName='zhangsan' and (userName='zhangsan' or password='123456')
        qw.eq("userName", userInfo.getUserName())
                .and(w -> w.eq("userName", userInfo.getUserName()).or()
                        .eq("password", userInfo.getPassword()));

        return this.getOne(qw);
    }

    @Override
    public boolean addUser(UserInfo userInfo) {
        User user = new User();
        BeanUtils.copyProperties(userInfo, user);
        return this.save(user);
    }

    @Override
    public boolean addUserBatch(UserInfo userInfo) {
        User user = new User();
        BeanUtils.copyProperties(userInfo, user);

        List<User> userList = new ArrayList<>();
        userList.add(user);

        return this.saveBatch(userList);
    }

    /**
     * 对象中有id字段时，使用updateById
     *
     * @param userInfo
     * @return
     */
    @Override
    public boolean updateUser(UserInfo userInfo) {

        User user = new User();
        BeanUtils.copyProperties(userInfo, user);

        return this.updateById(user);
    }

    /**
     * update(Object,Wrapper)
     * 在wrapper定义任意字段的条件构造器
     *
     * @param userInfo
     * @return
     */
    @Override
    public boolean updateUserByWrapper(UserInfo userInfo) {

        User user = new User();
        user.setUserName(userInfo.getUserName());
        user.setPassword(userInfo.getPassword());

//        相当于：where userName='值'
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("userName", userInfo.getUserName());

        return this.update(user, wrapper);
    }

    @Override
    public boolean updateUserBatch(UserInfo userInfo) {

        User user = new User();
        BeanUtils.copyProperties(userInfo, user);

        List<User> userList = new ArrayList<>();
        userList.add(user);

//        根条件构造器批量更新
//        this.updateUserByWrapper(wrapper);

        return this.updateBatchById(userList);
    }

    @Override
    public Page<User> getUserPage(UserPageInfo userPageInfo) {

        Page<User> page = new Page<>();
//        每页多少条数据
        page.setSize(userPageInfo.getPageSize());
//        第几页
        page.setCurrent(userPageInfo.getPageNumber());

        return this.page(page);
    }

    @Override
    public UserRole getUserRoleByUserId(Integer userId) {

        return userMapper.selectUserRoleByUserId(userId);
    }

    @Override
    public boolean deleteUser(Integer id) {
        return this.removeById(id);
    }

    @Override
    public User getUserInfoByName(String userName) {
        QueryWrapper<User> w = new QueryWrapper<>();
        w.eq("userName", userName);
        return this.getOne(w);
    }
}
